<?php
	/**
	 * String parser class
	 * @package Classes
	 * @author Oleg Svarog
 	 * @version 1.0
 	 * 
	 */

	/**
	 * This class is for working with strings
	 * @package Classes
	 */
	class String
	{
		/**
	     * Return a string, where the last comma is erased
	     * @param string $string 
	     * @return string $string
	     */
		static function RemoveLastComma($string)
		{
			$string = substr_replace($string, '', strrpos($string, ','), 1);
			return $string;
		}
		
		/**
	     * Return a string, where the last comma is erased
	     * @param string $string 
	     * @return string $string
	     */
		static function RemoveBorderCommas($string)
		{
			$string = preg_replace("[^,]", "", $string);
			$string = preg_replace("[,$]", "", $string);
			return $string;
		}
		
		/**
	     * Return a string, where the last $substring is erased
	     * @param string $string 
	     * @param string $substring
	     * @return string $string
	     */
		static function RemoveLastSubstring($string, $substring)
		{
			$q = strrpos($string, $substring);
			$w = strlen($substring);
			$string = substr_replace($string, '', strripos($string, $substring), strlen($substring));
			return $string;
		}
	
		/**
		 * Generates the human readable password
		 * @param $length
		 * @return String
		 */
		static function GenerateReadablePassword($length = 6)  
		{  
			$c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";  
			srand((double)microtime()*1000000);  
			
			for ($i = 0; $i < $length; $i++)  
			{  
		    	$rand .= $c[rand()%strlen($c)];  
			}  
			return $rand;  
		} 	
	 
		/**
		 * DEPRICATED: Translates the string with the parameters (%i)
		 * Use Language::Get with parameters (3rd argument) instead
		 * @param string $text the text to translate
		 * @param array $parameters parameters
		 */
	    static function Translate($text, array $parameters = array())
	    {
	        if (!empty($parameters))
	        {
    	        $i = 1;
                foreach ($parameters as $parameter)
                {
                    $text = str_replace('{%'.$i.'}', $parameter, $text);
                    $i++;
                }
	        }
                
	        return $text;
	    }
	
		/**
		 * Parses into given date(-time) format
		 *
		 * @param string $date
		 * @param string $format
		 */
		static function ToDateFormat($date, $format = "Y-m-d H:i:s")
		{
			$date = new DateTime($date);
			return date_format($date, $format);
		}
	
		/**
		 * Completes given string to the given length with the given symbol
		 *
		 * @param string $string string to parse
		 * @param int $requiredLength required length
		 * @param string $stringToAdd string to be added
		 */
		static function CompleteToLength($string, $requiredLength, $stringToAdd)
		{
			$stringLength = strlen($string);
			for ($j = $stringLength; $j < $requiredLength; $j++)
			{
				$string = $string.$stringToAdd;
			}
			
			return $string;
		}
	}
?>